<template>
  <div class="p-30">
    <van-cell-group inset>
      <van-field
        v-model="user.username"
        label="账号"
        placeholder="请输入账号"
      />
      <van-field
        v-model="user.password"
        label="密码"
        placeholder="请输入密码"
      />
    </van-cell-group>
    <van-button type="primary" block class="m-t-10" @click="handleLogin">登录</van-button>
    <div class="p-30">
      <router-link to="/reg">立即注册</router-link>
    </div>
  </div>
</template>

<script setup>
import { reactive } from "@vue/reactivity";
import { Notify } from "vant";
import { userLogin } from '../api/user'
import { useRouter,useRoute } from 'vue-router'
import { useStore } from 'vuex'
let router = useRouter()
let route = useRoute()
let store = useStore()
let user = reactive({
  username:'张三丰',
  password:'123'
})
// console.log(route.query);
let handleLogin = ()=>{
  userLogin(user).then(res=>{
    // console.log(res);
    let {code,data} = res.data
    if(code==111){ //成功
      console.log(code);
      store.commit('user/initInfoMut',data) //将用户信息存入状态机
      localStorage.setItem('userInfo',JSON.stringify(data))
      store.dispatch('cart/cartInitAct',data._id)  //初始化购物车
      let {target} = route.query
      if(target=='/detail'){
        router.back()
      }else{
        router.push(target)
      }
    }
  })
}
</script>

<style lang="less" scoped>

</style>